(deftemplate stare
    (slot valc1)
    (slot valc2)
    (slot nrOrdine)
    (slot available(default TRUE))
    )



(assert(stare(valc1 0)(valc2 0)(nrOrdine 0)));starea iniiala

(deftemplate solutie
    (slot nrOrdine)
    )
(assert(solutie(nrOrdine 0)))
(deftemplate nodeCount (slot x))

(assert(nodeCount(x 0)))

(defrule check
    (declare (salience 20))
    (solutie(nrOrdine ?y & 0))
    ?f1<-(stare(valc1 ?c1)(valc2 ?c2)(nrOrdine ?nr1))
     ?f2<-(stare(valc1 ?c1)(valc2 ?c2)(nrOrdine ?nr2))
    (test(> ?nr2 ?nr1))
    =>
    (modify ?f2 (available FALSE))
    )

(defrule identificareStareFinala
    (declare(salience 10))
    (solutie(nrOrdine ?y & 0))
    (stare(valc1 ?c1)(valc2 2)(nrOrdine ?nr))
    ?f<-(solutie(nrOrdine ?n))
    =>
    (modify ?f (nrOrdine ?nr))
    )

(defrule umplere1
    (declare(salience 5))
    (solutie(nrOrdine ?y & 0))
    ?fi<-(nodeCount (x ?xn))
    (stare(valc1 ?c1)(valc2 ?c2)(nrOrdine ?n))
    (test (< ?c1 3))
    =>
    (bind ?nr(+ ?xn 1))
    (assert(stare(valc1 3)(valc2 ?c2)(nrOrdine ?nr)))
    (printout t "Stare nr:" ?nr " c1=3 c2=" ?c2  crlf)
    (modify ?fi(x ?nr))
    )

(defrule umplere2
    (declare(salience 0))
    (solutie(nrOrdine ?y & 0))
    ?fi<-(nodeCount (x ?xn))
    (stare(valc1 ?c1)(valc2 ?c2)(nrOrdine ?n))
    (test (< ?c2 4))
    =>
    (bind ?nr(+ ?xn 1))
    (assert(stare(valc1 ?c1)(valc2 4)(nrOrdine ?nr)))
     (printout t "Stare nr:" ?nr " c1=" ?c1 " c2=4 "  crlf)
    (modify ?fi(x ?nr))
    )
(defrule golire1
    (declare(salience -10))
    (solutie(nrOrdine ?y & 0))
    ?fi<-(nodeCount (x ?xn))
    (stare(valc1 ?c1)(valc2 ?c2)(nrOrdine ?n))
    (test (> ?c1 0))
    =>
    (bind ?nr(+ ?xn 1))
    (assert(stare(valc1 0)(valc2 ?c2) (nrOrdine ?n)))
     (printout t "Stare nr:" ?nr " c1=0 c2=" ?c2   crlf)
    (modify ?fi(x ?nr))
    )

(defrule golire2
    (declare(salience -5))
    (solutie(nrOrdine ?y & 0))
    ?fi<-(nodeCount (x ?xn))
    (stare(valc1 ?c1)(valc2 ?c2)(nrOrdine ?n))
    (test (> ?c2 0))
    =>
    (bind ?nr (+ ?xn 1))
    (assert(stare(valc1 ?c1)(valc2 0)(nrOrdine ?n)))
     (printout t "Stare  nr:" ?nr " c1=" ?c1 " c2=0"  crlf)
    (modify ?fi(x ?nr))
    )
(defrule golire12
    (declare(salience -15))
    (solutie(nrOrdine ?y & 0))
    (stare(valc1 ?c1)(valc2 ?c2))
    (test (and(> ?c1 0)(< ?c2 4)))
    =>
  (bind ?cantitateCeTrebuieTransferata (- 4 ?c2))
   (if (< ?cantitateCeTrebuieTransferata ?c1)
        then(bind ?cantitateTransferata ?cantitateCeTrebuieTransferata)
        else(bind ?cantitateTransferata ?c1)
   )
    (assert(stare(valc1(- ?c1 ?cantitateTransferata))(valc2 (+ ?c1 ?cantitateTransferata))))
)

(defrule golire21
    (declare(salience -20))
    (solutie(nrOrdine ?y & 0))
    (stare(valc1 ?c1)(valc2 ?c2))
    (test (and(> ?c2 0)(< ?c1 3)))
    =>
  (bind ?cantitateCeTrebuieTransferata (- 3 ?c1))
   (if (< ?cantitateCeTrebuieTransferata ?c2)
        then(bind ?cantitateTransferata ?cantitateCeTrebuieTransferata)
        else(bind ?cantitateTransferata ?c2)
   )
     (assert(stare(valc2(- ?c2 ?cantitateTransferata))(valc1 (+ ?c2 ?cantitateTransferata))))
)
(run)

